﻿
Imports System.Data
Partial Class WebForm_AssetMaintain_Component_AssetMaintain
    Inherits BaseUserControl

    Public Event onSave(ByVal source As Object, ByVal e As EventArgs)
    Public Sub InitDataAndShow(ByVal asset_serialNo As String, ByVal canSave As Boolean)
        lblasset_serialNo1.Text = asset_serialNo
        ViewState("dbName") = dbName
        ViewState("UID") = ActualUID
        GetData()
        If Not canSave Then
            btnOK.Enabled = False
        End If
        Me.mpSearch.Show()
    End Sub
    Public Sub GetData()
        Dim bbl As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        If lblasset_serialNo1.Text = "" Then
            BlankData()
            Exit Sub
        End If
        Dim dt As DataTable = bbl.GetAssetData("", "", lblasset_serialNo1.Text, "", "", "", "", "", "", ViewState("dbName"))
        Dim dr As DataRow = dt.Rows(0)
        Try
            ddlassetdef_id.SelectedValue = dr("assetdef_id").ToString().Trim
        Catch ex As Exception

        End Try

        txtasset_owner.Text = dr("asset_owner").ToString().Trim
        lblownerName.Text = dr("ownerName").ToString.Trim
        txtEXT.Text = dr("EXT").ToString().Trim
        Try
            ddlasset_status.SelectedValue = dr("asset_status").ToString.Trim
        Catch ex As Exception

        End Try

        txtDept.Text = dr("Dept").ToString().Trim
        txtMAC1.Text = dr("MAC1").ToString().Trim
        txtMAC2.Text = dr("MAC2").ToString().Trim
        Try
            ddlFactory.SelectedValue = dr("Factory").ToString().Trim
        Catch ex As Exception

        End Try

        txtClass.Text = dr("Class").ToString().Trim
        txtLocation.Text = dr("Location").ToString().Trim
        txtAsset_Cost.Text = dr("Asset_Cost").ToString.Trim
        Try
            ddlHouseID.SelectedValue = dr("HouseID").ToString().Trim
        Catch ex As Exception

        End Try

        txtComputerName.Text = dr("ComputerName").ToString.Trim
        txtModel.Text = dr("Model").ToString.Trim
        txtCPU.Text = dr("CPU").ToString.Trim
        txtCD_R.Text = dr("CD_R").ToString.Trim
        txtCD_RW.Text = dr("CD_RW").ToString.Trim
        txtRAM.Text = dr("RAM").ToString.Trim
        Try
            ddlUsingType.SelectedValue = dr("UsingType").ToString.Trim
        Catch ex As Exception

        End Try

        txtOS.Text = dr("OS").ToString.Trim
        txtHDD.Text = dr("HDD").ToString.Trim
        txtMonitorSize.Text = dr("MonitorSize").ToString.Trim
        txtRemark.Text = dr("Remark").ToString.Trim
        txtOtherRemark.Text = dr("OtherRemark").ToString.Trim
        lblCrtName.Text = dr("CrtName").ToString.Trim
        Try
            lblCrtDate1.Text = DateTime.Parse(dr("CrtDate").ToString).ToString("yyyy/MM/dd HH:mm:ss")
        Catch ex As Exception
            lblCrtDate1.Text = ""
        End Try
        lblModiName.Text = dr("ModiName").ToString.Trim
        Try
            lblModiDate1.Text = DateTime.Parse(dr("ModiDate").ToString).ToString("yyyy/MM/dd HH:mm:ss")
        Catch ex As Exception
            lblModiDate1.Text = ""
        End Try
        txtStoreLocation.Text = dr("StoreLocation").ToString.Trim
        txtUser1.Text = dr("User1").ToString.Trim
        Try
            ddlUsingType2.SelectedValue = dr("UsingType2").ToString.Trim
        Catch ex As Exception

        End Try

        txtPS.Text = dr("PS").ToString.Trim
        Try
            ddlStoreHouse.SelectedValue = dr("StoreHouse").ToString.Trim
        Catch ex As Exception

        End Try

        txtNBLabel.Text = dr("NBLabel").ToString.Trim()
        txtFDD.Text = dr("FDD").ToString.Trim
        txtUsingDesc.Text = dr("UsingDesc").ToString.Trim



        '不是Admin
        Dim grpBBL As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()
        If Not grpBBL.GroupMemberIsExist("Admin", ViewState("UID").ToString, ViewState("dbName").ToString) Then
            ddlassetdef_id.Enabled = False
            txtasset_owner.Enabled = False

            txtEXT.Enabled = False
            ddlasset_status.Enabled = False
            txtDept.Enabled = False
            txtMAC1.Enabled = False
            txtMAC2.Enabled = False
            ddlFactory.Enabled = False
            txtClass.Enabled = False
            txtLocation.Enabled = False
            txtAsset_Cost.Enabled = False
            ddlHouseID.Enabled = False
            txtComputerName.Enabled = False
            txtModel.Enabled = False
            txtCPU.Enabled = False
            txtCD_R.Enabled = False
            txtCD_RW.Enabled = False
            txtRAM.Enabled = False
            ddlUsingType.Enabled = False
            txtOS.Enabled = False
            txtHDD.Enabled = False
            txtMonitorSize.Enabled = False
            txtRemark.Enabled = False
            txtPS.Enabled = False
            txtOtherRemark.Enabled = False
            txtNBLabel.Enabled = False
            txtFDD.Enabled = False
        End If
        If grpBBL.GroupMemberIsExist("Admin", ActualUID, dbName) = True Then
            btnOK.OnClientClick = "return confirm ('你確定要將資產狀態改為:" + ddlasset_status.SelectedItem.Text + "?');"
        End If

    End Sub
    Private Sub BlankData()


        txtasset_owner.Text = ""
        lblownerName.Text = ""
        txtEXT.Text = ""

        txtDept.Text = ""
        txtMAC1.Text = ""
        txtMAC2.Text = ""
        txtClass.Text = ""
        txtLocation.Text = ""
        txtAsset_Cost.Text = ""
        txtComputerName.Text = ""
        txtModel.Text = ""
        txtCPU.Text = ""
        txtCD_R.Text = ""
        txtCD_RW.Text = ""
        txtRAM.Text = ""
        txtOS.Text = ""
        txtHDD.Text = ""
        txtMonitorSize.Text = ""
        txtRemark.Text = ""
        txtOtherRemark.Text = ""
        lblCrtName.Text = ""
      
        lblCrtDate1.Text = ""
       
        lblModiDate1.Text = ""

        txtStoreLocation.Text = ""
        txtUser1.Text = ""


        txtPS.Text = ""
        txtNBLabel.Text = ""
        txtFDD.Text = ""
        txtUsingDesc.Text = ""
    End Sub
  
    Private Function ChkData() As Boolean
        Dim MAC1 As String = txtMAC1.Text.Replace(":", "").Replace("-", "").Replace(".", "").ToUpper()
        Dim MAC2 As String = txtMAC2.Text.Replace(":", "").Replace("-", "").Replace(".", "").ToUpper()
        If MAC1.Length <> 12 And MAC1.Length <> 0 Then

            ShowMessageForAjax(btnCancel, GetLocalResourceObject("MACError"))
            Return False
        End If
        If MAC2.Length <> 12 And MAC2.Length <> 0 Then
            ShowMessageForAjax(btnCancel, GetLocalResourceObject("MACError"))
            Return False
        End If
        'If ddlasset_status.SelectedValue = "Distributed" Then
        '    Try
        '        Decimal.Parse(txtAsset_Cost.Text)
        '    Catch ex As Exception
        '        ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_Cost"))
        '        Return False
        '    End Try
        'Else
        '    If txtAsset_Cost.Text <> "" Then
        '        Try
        '            Decimal.Parse(txtAsset_Cost.Text)
        '        Catch ex As Exception
        '            ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_Cost"))
        '            Return False
        '        End Try
        '    Else
        '        'txtAsset_Cost.Text = "0"
        '    End If

        'End If

        If txtAsset_Cost.Text <> "" Then ''有輸入才檢查
            Try
                Decimal.Parse(txtAsset_Cost.Text)
            Catch ex As Exception
                ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_Cost"))
                Return False
            End Try
        End If

        If ddlasset_status.SelectedValue = "Distributed" Then   'inused 需檢查人員是否存在
            Dim ubbl As OAMS.BBL.UsersBBL = New OAMS.BBL.UsersBBL()

            If ubbl.UserIsQuit(txtasset_owner.Text, dbName) = True Then ''離職

                ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_OwnerQuit"))
                Return False
            End If
            If ubbl.UserIsExist(txtasset_owner.Text, dbName) = False Then ''沒人

                ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_Owner"))
                Return False
            End If
        Else
            If txtasset_owner.Text <> "IT" Then
                ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrAsset_Owner"))
                Return False
            End If
        End If




            Return True
    End Function

    Protected Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
        If ddlUsingType2.SelectedValue = "9999" Then
            If txtUsingDesc.Text.Trim = "" Then
                ShowMessageForAjax(btnCancel, GetLocalResourceObject("ErrNeedUsingDesc"))
                mpSearch.Show()
                Exit Sub
            End If
        End If
        Dim housetypeBBL As AssetManage.BBL.HouseTypeBBL = New AssetManage.BBL.HouseTypeBBL()
        Dim housetype As String
        Try
            housetype = housetypeBBL.GetHouseType(ddlStoreHouse.SelectedValue, "", dbName).Rows(0)("House_Type").ToString()
        Catch ex As Exception
            housetype = "1"   '找不到則定為個人
        End Try

        '不是Admin
        Dim grpBBL As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()
        Dim aBBL As AssetManage.BBL.AssetBBL = New AssetManage.BBL.AssetBBL()
        If Not grpBBL.GroupMemberIsExist("Admin", ViewState("UID").ToString, ViewState("dbName").ToString) Then
            aBBL.SaveAssetData(ddlUsingType2.SelectedValue, ddlStoreHouse.SelectedValue, txtStoreLocation.Text, txtUser1.Text, ViewState("UID").ToString, lblasset_serialNo1.Text, ViewState("dbName").ToString, txtUsingDesc.Text)
        Else
            ''如果沒有輸入價格則抓取設定檔
            Dim cost As String
            cost = aBBL.GetAssetCost("", txtModel.Text, dbName)
            'If txtAsset_Cost.Text.Trim = "" Then
            '   txtAsset_Cost.Text = aBBL.GetAssetCost(ddlFactory.SelectedValue, txtModel.Text, dbName))
            'End If

            '2011/7/18 修改成要抓COST資料
            If ddlasset_status.SelectedValue = "Distributed" Then
                If cost <> "" Then
                    txtAsset_Cost.Text = cost
                End If
            End If


            ''資料檢查時會檢查是否輸入金額
            If Not ChkData() Then
                mpSearch.Show()
                Exit Sub
            End If
            Dim vo As AssetManage.VO.AssetVO = New AssetManage.VO.AssetVO()
            vo.asset_serialNo = lblasset_serialNo1.Text
            vo.assetdef_id = ddlassetdef_id.SelectedValue
            vo.asset_owner = txtasset_owner.Text

            vo.asset_status = ddlasset_status.SelectedValue
            Dim uDao As OAMS.DAO.UsersDAO = New OAMS.DAO.UsersDAO(dbName)
            If ddlasset_status.SelectedValue = "Distributed" Then ''2011/8/16日 修改為只有使用中的資料才需將user相關資料修改
                Try
                    Dim dr = uDao.GetEmpData(txtasset_owner.Text).Rows(0)
                    vo.Ownerid = dr("login_id").ToString
                    vo.UserName = dr("UserName").ToString
                    vo.Dept = dr("Dept_ID").ToString()
                    vo.EXt = dr("Ext_no").ToString()
                Catch ex As Exception
                    vo.Ownerid = ""
                    vo.UserName = ""
                    vo.Dept = ""
                    vo.EXt = ""
                End Try

            End If




            vo.MAC1 = txtMAC1.Text.Replace(":", "").Replace("-", "").Replace(".", "").ToUpper()
            vo.MAC2 = txtMAC2.Text.Replace(":", "").Replace("-", "").Replace(".", "").ToUpper()
           
            vo.Factory = ddlFactory.SelectedValue
            vo.Class1 = txtClass.Text
            vo.Location = txtLocation.Text
            vo.Asset_Cost = txtAsset_Cost.Text
            vo.HouseID = housetype

            vo.ComputerName = txtComputerName.Text
            vo.Model = txtModel.Text
            vo.CPU = txtCPU.Text
            vo.CD_R = txtCD_R.Text
            vo.CD_RW = txtCD_RW.Text
            vo.RAM = txtRAM.Text
            vo.OS = txtOS.Text
            vo.HDD = txtHDD.Text
            vo.UsingType = ddlUsingType.SelectedValue
            vo.MonitorSize = txtMonitorSize.Text
            vo.StoreLocation = txtStoreLocation.Text
            vo.storehouse = ddlStoreHouse.SelectedValue
            vo.UsingType2 = ddlUsingType2.SelectedValue
            vo.PS = txtPS.Text
            vo.Remark = txtRemark.Text


            
            vo.User1 = txtUser1.Text
            vo.OtherRemark = txtOtherRemark.Text
            vo.NBLabel = txtNBLabel.Text
            vo.FDD = txtFDD.Text
            vo.UsingDesc = txtUsingDesc.Text
            Dim webPath As String
            webPath = String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/") _
            + "/WebForm/Main/MainPage.aspx?path=" + String.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority, HttpContext.Current.Request.ApplicationPath).TrimEnd("/") + _
            "/webForm/AssetMaintain/AssetMaintain.aspx?Asset_serialNo=" + lblasset_serialNo1.Text
            If lblasset_serialNo1.Text = "" Then
                vo.CrtDate = Now.ToString("yyyy/MM/dd HH:mm:ss")
                vo.CrtUID = ViewState("UID").ToString
                aBBL.InsertAssetData(vo, webPath, ViewState("dbName").ToString)
            Else
                vo.ModiDate = Now.ToString("yyyy/MM/dd HH:mm:ss")
                vo.ModiUID = ViewState("UID").ToString
                aBBL.SaveAssetData(vo, ViewState("dbName").ToString, webPath)

            End If
         
        End If


        RaiseEvent onSave(sender, New EventArgs())
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click

    End Sub

    Protected Sub ddlUsingType2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlUsingType2.SelectedIndexChanged
      
    End Sub

    Protected Sub ddlasset_status_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlasset_status.SelectedIndexChanged
        Dim grpBBL As OAMS.BBL.GroupBBL = New OAMS.BBL.GroupBBL()
        If grpBBL.GroupMemberIsExist("Admin", ActualUID, dbName) = True Then
            btnOK.OnClientClick = "return confirm ('你確定要將資產狀態改為:" + ddlasset_status.SelectedItem.Text + "?');"
        End If

        Me.mpSearch.Show()
    End Sub
End Class
